package com.rjwh.dingdong.client.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RatingBar;
import android.widget.TextView;

import com.aishua.lib.utils.AsStrUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.rjwh.dingdong.client.R;
import com.rjwh.dingdong.client.bean.localbean.MathGameFootStepListData;
import com.rjwh.dingdong.client.util.ViewHolder;
import com.rjwh.dingdong.client.widget.RoundImageView;

/**
 * @author Kay
 * @ClassName MathGameMyTracksGameAdapter
 * @Description
 * @date 2016-8-31
 * @history 2016-8-31 author: description:
 */
public class MathGameMyTracksGameAdapter extends AdapterBase<MathGameFootStepListData> {

    private Context context;
    private int currentPosition = -1;
    private OnItemButtonClickListener buttonClickListener;

    public MathGameMyTracksGameAdapter(Context context, OnItemButtonClickListener buttonClickListener) {
        this.context = context;
        this.buttonClickListener = buttonClickListener;
    }

    @Override
    protected View getExView(final int position, View convertView, ViewGroup parent) throws Exception {
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.math_game_my_tracks_item, null);
        }
        RoundImageView img = ViewHolder.get(convertView, R.id.math_game_my_tracks_item_img);
        RatingBar ratingBar = ViewHolder.get(convertView, R.id.math_game_my_tracks_item_rating_bar);
        TextView titleTv = ViewHolder.get(convertView, R.id.math_game_my_tracks_item_title);
        TextView descTv = ViewHolder.get(convertView, R.id.math_game_my_tracks_item_desc);
        Button startBtn = ViewHolder.get(convertView, R.id.math_game_my_tracks_item_btn);

        MathGameFootStepListData mathGameFootStepListData = getList().get(position);
        if (mathGameFootStepListData == null) {
            return convertView;
        }

        String gameimage = mathGameFootStepListData.getGameimage();
        String gamename = mathGameFootStepListData.getGamename();
        String gamedesc = mathGameFootStepListData.getGamedesc();
        String star = mathGameFootStepListData.getStar();
        final String bookid = mathGameFootStepListData.getBookid();
        final String lessonid = mathGameFootStepListData.getLessonid();
        final String gamedir = mathGameFootStepListData.getGamedir();
        final String gameid = mathGameFootStepListData.getGameid();

        Glide.with(context).load(gameimage).dontAnimate().placeholder(R.drawable.icon_pic_holder).error(R.drawable.icon_pic_holder)
                .diskCacheStrategy(DiskCacheStrategy.ALL).into(img);

        if (!AsStrUtils.isEmpty(gamename)) {
            titleTv.setText(gamename);
        }
        if (!AsStrUtils.isEmpty(gamedesc)) {
            descTv.setText(gamedesc);
        }
        if (star != null) {
            ratingBar.setRating(Integer.parseInt(star));
        }

        descTv.setVisibility(currentPosition == position ? View.VISIBLE : View.GONE);
        startBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                buttonClickListener.onItemButtonClick(bookid, lessonid, gamedir, gameid);
            }

        });
        return convertView;
    }

    public void setSelectPosition(int currentPosition) {
        this.currentPosition = currentPosition;
    }

    public interface OnItemButtonClickListener {
        void onItemButtonClick(String bookid, String lessonid, String gamedir, String gameid);
    }
}
